/**
 * User: laice
 * Date: 9/27/13
 * Time: 12:13 AM
 */

var socket = io.connect();

var MY_ID;

var p1_sw = false;
var p1_sl = false;
var p1_sc = false;
var p2_sw = false;
var p2_sl = false;
var p2_sc = false;
var p1_st = false;
var p2_st = false;

var p1_win_elo = 0;
var p1_loss_elo = 0;
var p2_win_elo = 0;
var p2_loss_elo = 0;


function match_clicked() {
    var data = "current_match"
    socket.emit('mode', { mode : data });
}

function roundly_cleaning(){
    $('#player_one').find('*').each(function(){

        $(this).text("");
    });

    $('#player_two').find('*').each(function() {
       $(this).text("");
    });
    p1_sw = false;
    p1_sl = false;
    p1_sc = false;
    p2_sw = false;
    p2_sl = false;
    p2_sc = false;
    p1_st = false;
    p2_st = false;

    p1_elo_wins = [];
    p1_elo_losses = [];
    p2_elo_wins = [];
    p2_elo_losses = [];
}

var p1_elo_wins = [];
var p1_elo_losses = [];
var p2_elo_wins = [];
var p2_elo_losses = [];

function assign_elo(value, player, pos){

    if(player == 1){
        if(pos == "win"){
            p1_elo_wins.push(value)
        } else if ( pos == "loss"){
            p1_elo_losses.push(value)
        }
    } else if ( player == 2 ){
        if(pos == "win"){
            p2_elo_wins.push(value);
        } else if ( pos == "loss"){
            p2_elo_losses.push(value);
        }
    }

    update_elo();
}

function update_elo(){

    var sum = 0;

    for(var i = 0; i < p1_elo_wins.length-1; i++ ){
        socket.emit('debug', {output: p1_elo_wins[i]});
        sum = sum + p1_elo_wins[i];
    }

    var avg = sum/p1_elo_wins.length;

    $('#elo_win_sum_one').text(sum);
    $('#elo_win_mean_one').text(avg);

    sum = 0;

    for(var i = 0; i < p1_elo_losses.length-1; i++ ){
        sum = sum + p1_elo_losses[i];
    }

    avg = sum/p1_elo_losses.length;
    $('#elo_loss_sum_one').text(sum);
    $('#elo_loss_mean_one').text(avg);

    sum = 0;

    for(var i = 0; i < p2_elo_wins.length-1; i++ ){
        sum = sum + p2_elo_wins[i];
    }

    var avg = sum/p2_elo_wins.length;

    $('#elo_win_sum_two').text(sum);
    $('#elo_win_mean_two').text(avg);

    sum = 0;

    for(var i = 0; i < p2_elo_losses.length-1; i++ ){
        sum = sum + p2_elo_losses[i];
    }

    avg = sum/p2_elo_losses.length;
    $('#elo_loss_sum_two').text(sum);
    $('#elo_loss_mean_two').text(avg);




}

function assign_teaser(p1, p2) {

    document.getElementById('match_loading').setAttribute("hidden", "true");
    var assign = new Array();

    assign.n1 = document.getElementById('name_one').innerHTML=p1.name;
    assign.n2 = document.getElementById('name_two').innerHTML=p2.name;

    assign.id1 = document.getElementById('id_one').innerHTML="ID: " +p1.id;
    assign.id2 = document.getElementById('id_two').innerHTML="ID: " +p2.id;

    assign.w1 = document.getElementById('wins_one').innerHTML="Wins: " + p1.wins;
    assign.w2 = document.getElementById('wins_two').innerHTML="Wins: " + p2.wins;

    assign.l1 = document.getElementById('losses_one').innerHTML="Losses: " + p1.losses;
    assign.l2 = document.getElementById('losses_two').innerHTML="Losses: " + p2.losses;

    assign.p1 = document.getElementById('percent_one').innerHTML="Percent: " + p1.percent;
    assign.p2 = document.getElementById('percent_two').innerHTML="Percent: " + p2.percent;

    assign.e1 = document.getElementById('e_one').innerHTML="E: " + p1.e;
    assign.e2 = document.getElementById('e_two').innerHTML="E: " + p2.e;

    /*

    $('#elo_win_mean_one').append('0');
    $('#elo_win_median_one').append('0');
    $('#elo_loss_mean_one').append('0');
    $('#elo_loss_median_one').append('0');
    $('#elo_win_mean_two').append('0');
    $('#elo_win_median_two').append('0');
    $('#elo_loss_mean_two').append('0');
    $('#elo_loss_median_two').append('0');
    */

    assign.t1 = document.getElementById('total_matches_one').innerHTML="TM: " + p1.total_matches;
    assign.t2 = document.getElementById('total_matches_two').innerHTML="TM: " + p2.total_matches;

}

function assign_attr(p1, p2) {
    if(p1.aggression_index !== "null")
        document.getElementById('aggr_ind_1').innerHTML="AggInd: " + p1.aggression_index;
    if(p2.aggression_index !== "null")
        document.getElementById('aggr_ind_2').innerHTML="AggInd: " + p2.aggression_index;

    if(p1.total_bets !== "null")
        document.getElementById('total_bets_1').innerHTML="#Bets: " + p1.total_bets;
    if(p2.total_bets !== "null")
        document.getElementById('total_bets_2').innerHTML="#Bets: " + p2.total_bets;

    if(p1.created !== "null")
        document.getElementById('created_1').innerHTML="Created: " + p1.created;
    if(p2.created !== "null")
        document.getElementById('created_2').innerHTML="Created: " + p2.created;

    if(p1.updated !== "null")
        document.getElementById('created_1').innerHTML="Updated: " + p1.updated;
    if(p2.updated !== "null")
        document.getElementById('created_2').innerHTML="Updated: " + p2.updated;

    if(p1.gets_hitboxed !== "null")
        document.getElementById('gets_hbox_1').innerHTML="Gets Hitboxed: " + p1.gets_hitboxed;
    if(p2.gets_hitboxed !== "null")
        document.getElementById('gets_hbox_2').innerHTML="Gets Hitboxed: " + p2.gets_hitboxed;

    if(p1.undersized_hitbox !== "null")
        document.getElementById('undr_hbox_1').innerHTML="Und.H.Box: " + p1.undersized_hitbox;
    if(p2.undersized_hitbox !== "null")
        document.getElementById('undr_hbox_2').innerHTML="Und.H.Box: " + p2.undersized_hitbox;

    if(p1.oversized_hitbox !== "null")
        document.getElementById('over_hbox_1').innerHTML="Ovr.H.Box: " + p1.oversized_hitbox;
    if(p2.oversized_hitbox !== "null")
        document.getElementById('over_hbox_2').innerHTML="Ovr.H.Box: " + p2.oversized_hitbox;

    if(p1.zoning !== "null")
        document.getElementById('zoning_1').innerHTML="Zoning: " + p1.zoning;
    if(p2.zoning !== "null")
        document.getElementById('zoning_2').innerHTML="Zoning: " + p2.zoning;

    if(p1.rushdown !== "null")
        document.getElementById('rushdown_1').innerHTML="Rushdown: " + p1.rushdown;
    if(p2.rushdown !== "null")
        document.getElementById('rushdown_2').innerHTML="Rushdown: " + p2.rushdown;

    if(p1.one_hit_knockout !== "null")
        document.getElementById('ohk_1').innerHTML="OHK: " + p1.one_hit_knockout;
    if(p2.one_hit_knockout !== "null")
        document.getElementById('ohk_2').innerHTML="OHK: " + p2.one_hit_knockout;

    if(p1.doesnt_super !== "null")
        document.getElementById('no_super_1').innerHTML="No Super: " + p1.doesnt_super;
    if(p2.doesnt_super !== "null")
        document.getElementById('no_super_2').innerHTML="No Super: " + p2.doesnt_super;

    if(p1.high_health !== "null")
        document.getElementById('high_health_1').innerHTML="HighHealth: " + p1.high_health;
    if(p2.high_health !== "null")
        document.getElementById('high_health_2').innerHTML="HighHealth: " + p2.high_health;

    if(p1.low_health !== "null")
        document.getElementById('low_health_1').innerHTML="LowHealth: " + p1.low_health;
    if(p2.low_health !== "null")
        document.getElementById('low_health_2').innerHTML="LowHealth: " + p2.low_health;

    if(p1.grappler !== "null")
        document.getElementById('grappler_1').innerHTML="Grappler: " + p1.grappler;
    if(p2.grappler !== "null")
        document.getElementById('grappler_2').innerHTML="Grappler: " + p2.grappler;

    if(p1.grapple_immune !== "null")
        document.getElementById('g_immune_1').innerHTML="GrapImmune: " + p1.grapple_immune;
    if(p2.grapple_immune !== "null")
        document.getElementById('g_immune_2').innerHTML="GrapImmune: " + p2.grapple_immune;

    if(p1.flies !== "null")
        document.getElementById('flies_1').innerHTML="Flies: " + p1.flies;
    if(p2.flies !== "null")
        document.getElementById('flies_2').innerHTML="Flies: " + p2.flies;

    if(p1.heals !== "null")
        document.getElementById('heals_1').innerHTML="Heals: " + p1.heals;
    if(p2.heals !== "null")
        document.getElementById('heals_2').innerHTML="Heals: " + p2.heals;

    if(p1.timescammer !== "null")
        document.getElementById('timescam_1').innerHTML="Timescam: " + p1.timescammer;
    if(p2.timescammer !== "null")
        document.getElementById('timescam_2').innerHTML="Timescam: " + p2.timescammer;

    if(p1.has_fakes !== "null")
        document.getElementById('fakes_1').innerHTML="Fakes: " + p1.has_fakes;
    if(p2.has_fakes !== "null")
        document.getElementById('fakes_2').innerHTML="Fakes: " + p2.has_fakes;

}

function assign_p1_comm(data) {

    $('#comments_1').append('<div data-role="collapsible"><ul>');
    for(var i in data) {
        $('#comments_1').append("<li>" + data[i] + "</li>");

    }
    $('#comments_1').append('</ul></div>');

}

function assign_p2_comm(data) {
    $('#comments_2').append('<div data-role="collapsible"><ul>');
    for(var i in data) {
        $('#comments_2').append("<li>" + data[i] + "</li>");
    }
    $('#comments_2').append('</ul></div>');

}

function assign_p1_wins(data) {
    $('#wins_1').append('<div data-role="collapsible"><ul>');
    for(var i in data) {
        $('#wins_1').append("<li>" + data[i] + "</li>");
    }
    $('#wins_1').append('</ul></div>');

}

function assign_p2_wins(data) {
    $('#wins_2').append('<div data-role="collapsible"><ul>');
    for(var i in data) {
        $('#wins_2').append("<li>" + data[i] + "</li>");
    }
    $('#wins_2').append('</ul></div>');

}

function assign_p1_loss(data) {
    $('#losses_1').append('<div data-role="collapsible"><ul>');
    for(var i in data) {
        $('#losses_1').append("<li>" + data[i] + "</li>");
    }
    $('#losses_1').append('</ul></div>');

}

function assign_p2_loss(data) {
    $('#losses_2').append('<div data-role="collapsible"><ul>');
    for(var i in data) {
        $('#losses_2').append("<li>" + data[i] + "</li>");
    }
    $('#losses_2').append('</ul></div>');

}

function assign_p1_tags(data){
    $('#tags_one').append('<div data-role="collapsible"><ul>');
    for(var i in data){
        $('#tags_one').append("<li>" + data[i] + "</li>");
    }

    $('#tags_one').append('</ul></div>');
}

function assign_p2_tags(data){
    $('#tags_two').append('<div data-role="collapsible"><ul>');
    for(var i in data){
        $('#tags_two').append("<li>" + data[i] + "</li>");
    }

    $('#tags_two').append('</ul></div>');
}


$(function() {
   $("#menu_match").click(function(){match_clicked()});
});

socket.on('match_status', function(data) {
    switch(data.status){
        case"open":
            $('#match_status').css("color", "green");
            $('#match_status').text("Betting is Open!");
            break;
        case"locked":
            $('#match_status').css("color", "brown");
            $('#match_status').text("Betting is Locked!");
            break;
        case"1":
            roundly_cleaning();
            $('#match_status').css("color", "red");
            $('#match_status').text("Player 1 has won!");

            break;
        case"2":
            roundly_cleaning();
            $('#match_status').css("color", "blue");
            $('#match_status').text("Player 2 has won!");
            break;
        default:
            break;
    }
});

socket.on('teaser', function(old_data) {
    var data = JSON.parse(old_data.data);
    //socket.emit('debug', JSON.stringify(data.p1));
    //socket.emit('debug', JSON.stringify(data.p2));
    socket.emit('debug', { output : "client teaser"});
    assign_teaser(data.p1, data.p2);
});

socket.on('attr', function(old_data) {
    var data = JSON.parse(old_data.data);
    socket.emit('debug', { output : "client attr"});
    assign_attr(data.p1, data.p2)
});

socket.on('p1_comm', function(old_data){
    if(!p1_sc){
        p1_sc = true;
        var data = JSON.parse(old_data.comments);
        assign_p1_comm(data);
    }
});

socket.on('p2_comm', function(old_data){
    if(!p2_sc) {
        p2_sc = true;
        var data = JSON.parse(old_data.comments);
        assign_p2_comm(data);
    }
});

socket.on('p1_win', function(old_data){
    if(!p1_sw){
        p1_sw = true;
        var data = JSON.parse(old_data.win);
        assign_p1_wins(data);
    }
});

socket.on('p2_win', function(old_data){
    if(!p2_sw){
        p2_sw = true;
        var data = JSON.parse(old_data.win);
        assign_p2_wins(data);
    }
});

socket.on('p1_loss', function(old_data){
    if(!p1_sl){
        p1_sl = true;
        var data = JSON.parse(old_data.loss);
        assign_p1_loss(data);
    }
});

socket.on('p2_loss', function(old_data){
    if(!p2_sl){
        p2_sl = true;
        var data = JSON.parse(old_data.loss);
        assign_p2_loss(data);

    }
});

socket.on('p1_tags', function(old_data){
    socket.emit('debug', {output : "client p1 tags received"})
    if(!p1_st){
        p1_st = true;
        var data = JSON.parse(old_data.loss);
        assign_p1_tags(data);

    }
});

socket.on('p2_tags', function(old_data){
    socket.emit('debug', {output : "client p2 tags received"})
    if(!p2_st){
        p2_st = true;
        var data = JSON.parse(old_data.loss);
        assign_p2_tags(data);

    }
});



socket.on('p1_loss_elo', function(data){
   socket.emit('debug', {output : "1 loss : " + data.elos});
    assign_elo(data.elos, 1, "loss")


});

socket.on('p1_win_elo', function(data){
   socket.emit('debug', {output : "1 win : " + data.elos});
    assign_elo(data.elos, 1, "win")

});

socket.on('p2_loss_elo', function(data){
   socket.emit('debug', {output : "2 loss : " + data.elos});
    assign_elo(data.elos, 2, "loss")

});

socket.on('p2_win_elo', function(data){
   socket.emit('debug', {output : "2 win : " + data.elos});
    assign_elo(data.elos, 2, "win")

});




socket.on('your_id', function(data) {
    MY_ID = data.id;
});

socket.emit('new_client');

/*
function addMessage(msg, pseudo) {
    $("#chatEntries").append('<div class="message"><p>' + pseudo + ' : ' + msg + '</p></div>');
}

function sentMessage() {
    if ($('#messageInput').val() != "")
    {
        socket.emit('message', $('#messageInput').val());
        addMessage($('#messageInput').val(), "Me", new Date().toISOString(), true);
        $('#messageInput').val('');
    }
}

function setPseudo() {
    if ($("#pseudoInput").val() != "")
    {
        socket.emit('setPseudo', $("#pseudoInput").val());
        $('#chatControls').show();
        $('#pseudoInput').hide();
        $('#pseudoSet').hide();
    }
}

socket.on('message', function(data) {
    addMessage(data['message'], data['pseudo']);
});


$(function() {
    $("#chatControls").hide();
    $("#pseudoSet").click(function() {setPseudo()});
    $("#submit").click(function() {sentMessage();});
});
    */